package com.util.security.Display;

/**
 * @ClassName: Crypt
 * @Author: commander
 * @Description: 加密
 * @Version: 1.0
 * @Date: 2016/12/18 18:22
 */
public class Crypt {
	private String text, key, ctext, otext;
	private int ctextl, keyl;
	private char[] ch1, ch3, ch4;
	private int[] in1;
	private char[][] ch2;
	private int ch2r;

	public Crypt(String text, String key) {
		this.text = text;
		this.key = key;
		String ctext = text.replaceAll(" ", "");//消去明文中空格
		ch1 = ctext.toCharArray();
		ctextl = ctext.length();
		keyl = key.length();
		if (ctextl % keyl == 0){//设置二维数组行大小
			ch2r = ctextl / keyl;
		} else {
			ch2r = ctextl / keyl + 1;
		}
		ch2 = new char[ch2r][keyl];
		for (int i = 0, j = 0; i < ch2r; i++){//明文存入二维数组中
			for (int k = 0; k < keyl && j < ctextl; k++) {
				ch2[i][k] = ch1[j];
				j++;
			}
		}
		in1 = new int[keyl];
		Keys x = new Keys(key);
		in1 = x.getkey();
		ch4 = new char[ch2r * keyl];//存储输出的字符数组
		for (int i = 0, k = 0; i < keyl; i++) {
			for (int j = 0; j < ch2r && k < (ch2r * keyl); j++) {
				ch4[k] = ch2[j][in1[i]];
				k++;
			}
		}
		otext = String.valueOf(ch4);
	}

	public String getcrypt() {
		return otext;
	}
}
/*
Ok11iXX4jVkphxNr3H8x/u+SMzMWbjNLQYnstWcMKsEiZp/v7KMtoT11jsmzKQMq620pUkreucPo
		4n3fcM/7WlRN3XXeWxWZwFpV3iJ7DbsGodKw9iaMiBm2ou/lbZW8qpfoIiUfj/AP6qt1aOC1s401*/
